<?php

namespace app;

use think\Model;


/**
 * 模型基类
 * Class BaseModel
 * @package app
 * @author ^2_3^王尔贝
 */
class BaseModel extends Model
{
    /**
     * find
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return BaseModel|array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public function _find($where=[], $field='*', $orderBy='')
    {
        return $this->where( $where )->order( $orderBy )->field( $field )->find();
    }

    /**
     * column
     * @param $where
     * @param string $field
     * @param string $index
     * @return array
     * @author ^2_3^王尔贝
     */
    public function _column($where, $field='', $index='')
    {
        if( empty( $field ) ) {
            return [];
        }

        return $this->where( $where )->column( $field, $index );
    }

    /**
     * listOnly
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @return BaseModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public function listOnly($where=[], $field='*', $orderBy='id desc')
    {
        $list = $this->field( $field )
            ->where( $where )
            ->order( $orderBy )
            ->select();
        return $list;
    }

    /**
     * listPage
     * @param array $where
     * @param string $field
     * @param int $page
     * @param string $orderBy
     * @param int $pageSize
     * @param array $whereOr
     * @return mixed
     * @author ^2_3^王尔贝
     */
    public function listPage($where=[], $field='*', $page=1, $orderBy='id desc', $pageSize=0, $whereOr=[])
    {
        if( empty( $pageSize ) ) {
            $pageSize = config('database.list_rows');
        }

        $query = $this->field( $field )
            ->where( $where );

        if( !empty( $whereOr ) ) {
            $query = $query->whereOr($whereOr);
        }

        $pageList = $query->order( $orderBy )
            //->fetchSql(true)
            ->paginate([
                "list_rows" => $pageSize,
                "page" => $page,
            ]);
        return $pageList;
    }

    /**
     * create
     * @param array $data
     * @return Model|static
     * @author ^2_3^王尔贝
     */
    public function _create($data = [])
    {
        return $this::create( $data );
    }

    /**
     * update
     * @param array $data
     * @param array $where
     * @return static
     * @author ^2_3^王尔贝
     */
    public function _update($data=[], $where=[])
    {
        return $this->where( $where )->update( $data );
    }

    /**
     * saveAll
     * @param array $data
     * @return \think\Collection
     * @throws \Exception
     * @author cenyebao^2_3^
     * @email 1142733640@qq.com
     */
    public function _saveAll($data = [])
    {
        return $this->saveAll( $data );
    }

    /**
     * delete
     * @param array $where
     * @return bool
     * @author ^2_3^王尔贝
     */
    public function _delete($where = [])
    {
        return $this->where($where)->delete();
    }
}